//
//  WH_HomeCollectionCell.m
//  App_6.23
//
//  Created by wanghao on 16/6/28.
//  Copyright © 2016年 wanghao. All rights reserved.
//

#import "WH_HomeCollectionCell.h"
#import "AppDelegate.h"
@interface WH_HomeCollectionCell ()

@property (nonatomic, strong) UIImageView *picImage;
@property (nonatomic, strong) UILabel *picLabel;
@property (nonatomic, strong) UILabel *nubLabel;
@property (nonatomic, strong) UILabel *labelB;

@end
@implementation WH_HomeCollectionCell

-(instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        [self createSubs];
    }
    
    return self;
}

-(void)createSubs{
    
    self.picImage = [[UIImageView alloc] init];
    //self.picImage.backgroundColor = ARCDOM_4;
    //self.picImage.image = [UIImage imageNamed:@"image1.jpg"];

    [self.contentView addSubview:_picImage];
    [self.picImage mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.top.mas_equalTo(@0);
        make.width.height.mas_equalTo(@(SCREEN_WIDTH/2));
    }];
    
    self.picLabel = [UILabel new];
    self.picLabel.text = @"Apple iPhone6s 16G 颜色随机";
    self.picLabel.font = [UIFont systemFontOfSize:15.0];
    self.picLabel.numberOfLines = 2;
    [self.contentView addSubview:_picLabel];
    [self.picLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(@5);
        make.top.equalTo(self.picImage.mas_bottom);
        make.right.mas_equalTo(@-5);
        
    }];
    
    
    UILabel *label = [UILabel new];
    label.text = @"揭晓进度 ";
    label.font = [UIFont systemFontOfSize:12.0];
    [self.contentView addSubview:label];
    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(self.picLabel);
        make.top.equalTo(self.picLabel.mas_bottom).offset(10);
        make.width.mas_equalTo(@(60*WID_ONE));
        make.height.mas_equalTo(@(25*HEI_ONE));
    }];
    
    self.nubLabel = [UILabel new];
    self.nubLabel.text = @"28%";
    self.nubLabel.textColor = [UIColor blueColor];
    self.nubLabel.font = [UIFont systemFontOfSize:12.0];
    [self.contentView addSubview:_nubLabel];
    [self.nubLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(label.mas_right).offset(5);
        make.top.width.height.equalTo(label);
    }];
    
    UILabel *labelA = [UILabel new];
    labelA.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1];
    labelA.layer.cornerRadius = 3;
    labelA.layer.masksToBounds = YES;
    [self.contentView addSubview:labelA];
    [labelA mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.picLabel);
        make.top.equalTo(self.picLabel.mas_bottom).offset(40);
        make.width.mas_equalTo(@(100*WID_ONE));
        make.height.mas_equalTo(@6);
        
    }];
    self.labelB = [UILabel new];
    _labelB.backgroundColor = [UIColor colorWithRed:0.99 green:0.79 blue:0.27 alpha:1];
    _labelB.layer.cornerRadius = 3;
    _labelB.layer.masksToBounds = YES;
    [self.contentView addSubview:_labelB];
    [self.labelB mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.picLabel);
        make.top.equalTo(self.picLabel.mas_bottom).offset(40);
        make.width.mas_equalTo(@0);
        make.height.mas_equalTo(@6);
        
    }];
    
    UIButton *addOrderBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //addOrderBtn.backgroundColor = ARCDOM_4;
    [addOrderBtn addTarget:self action:@selector(addOrder:) forControlEvents:UIControlEventTouchUpInside];
    [addOrderBtn setTitle:@"加入清单" forState:UIControlStateNormal];
    [addOrderBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    addOrderBtn.layer.cornerRadius = 3;
    addOrderBtn.layer.borderWidth = .3;
    addOrderBtn.layer.borderColor = [UIColor redColor].CGColor;
    addOrderBtn.titleLabel.font = [UIFont systemFontOfSize:11.f];
    [self.contentView addSubview:addOrderBtn];
    [addOrderBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(@-5);
        make.bottom.equalTo(labelA);
        make.height.mas_equalTo(@25);
        make.left.equalTo(labelA.mas_right).offset(10);
        
    }];
    
    UILabel *labX = [UILabel new];
    labX.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
    [self.contentView addSubview:labX];
    [labX mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.mas_equalTo(@0);
        make.bottom.mas_equalTo(@-1);
        make.height.mas_equalTo(@1);
    }];
    UILabel *labR = [UILabel new];
    labR.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
    [self.contentView addSubview:labR];
    [labR mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.bottom.mas_equalTo(@0);
        make.right.mas_equalTo(@0);
        make.width.mas_equalTo(@1);
    }];

}
-(void)addOrder:(UIButton *)btn{

    if (![ISLOGIN isEqualToString:@"1"]) {
        
        [self toLogin];

    }else{
        
        [self addoreder];
    }
    
    NSLog(@"product_id%@",self.cellHome.product_id);
}
- (void)toLogin{
    
    //UIApplication是系统单例,可以直接使用
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    
    appDelegate.tabbarMain.selectedIndex = 3;
    
}
//加入清单
-(void)addoreder{
    
    if ([ISLOGIN isEqualToString:@"1"]) {

        
        NSString *body =[NSString stringWithFormat:@"action=cart_add&user_id=%@&product_period=%@&product_num=1", NSUSERID,self.cellHome.product_period] ;
        [WH_AppTools postUrl:URLstr body:body result:HXJSON requestStyle:HXRequestString headerFile:nil success:^(id result) {
            NSLog(@"%@",result);
            NSDictionary *dic = result;
            NSString *status = [dic objectForKey:@"status"];
            NSString *message = [dic objectForKey:@"message"];
            if ([status isEqualToString:@"success"]) {
                
                [WSProgressHUD showImage:nil status:@"addCart" maskType:WSProgressHUDMaskTypeClear];

            }else{
                
                [WSProgressHUD showImage:nil status:message maskType:WSProgressHUDMaskTypeClear];
                
            }
            
        } failure:^(NSError *error) {
            
            
        }];

    }else{
        
        
        [self toLogin];
    }
}
-(void)setCellHome:(WH_Home *)cellHome{
    
    if (_cellHome != cellHome) {
        
        _cellHome = cellHome;
    }
    [self.picImage sd_setImageWithURL:[NSURL URLWithString:cellHome.product_image] placeholderImage:PLACEHOLDER_CELL_SAME];
    self.picLabel.text = cellHome.product_name;
    self.nubLabel.text = [NSString stringWithFormat:@"%ld%%",(long)self.cellHome.product_rate];

    [self.labelB mas_updateConstraints:^(MASConstraintMaker *make) {
       
        make.width.mas_equalTo(@((self.cellHome.product_rate)*WID_ONE));
        
    }];
}
@end
